package com.zktr.service;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.zktr.bean.UsersBean;
import com.zktr.dao.UserDao;
import com.zktr.pojo.PageBean;
import com.zktr.pojo.Users;

/**
 * 会员的业务处理：
 * @author 123
 *
 */
public class UserService {
	UserDao dao=new UserDao();
	/**
	 * 查询全部信息：
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<UsersBean> findAllUsers(int pc, int ps) {
		Map<Integer, List<Users>> selectBooks = dao.selectUsers(pc, ps);
		List<UsersBean> list2=new LinkedList<>();
		status(selectBooks.get(0), list2);
		PageBean<UsersBean> bean=new PageBean<UsersBean>();
		bean.setPc(pc);//页码
		bean.setPs(ps);//页大小
		bean.setBeanList(list2);
		bean.setTr(dao.selectOrderinfoCount());//总记录条数
		return bean;
	}
	/**
	 * 数据转换：
	 * @param list
	 * @param list2
	 */
	private void status(List<Users> list, List<UsersBean> list2) {	
		for (Users users : list) {
			UsersBean usersBean=new UsersBean();
			usersBean.setAddressid(String.valueOf(users.getAddressid()));
			usersBean.setBirthday(String.valueOf(users.getBirthday()));
			usersBean.setEmail(users.getEmail());
			usersBean.setInterest(users.getInterest());
			usersBean.setLastlogin(mydate(users.getLastlogin()));
			usersBean.setMobilephone(users.getMobilephone());
			usersBean.setPassword(users.getPassword());
			usersBean.setPaypoints(String.valueOf(users.getPaypoints()));
			usersBean.setRankpoints(String.valueOf(users.getRankpoints()));
			usersBean.setRegtime(mydate(users.getRegtime()));
			if(users.getSex()) {
				usersBean.setSex("男");
			}else {
				usersBean.setSex("女");
			}
			/*usersBean.setSex("男");*/
			usersBean.setUserid(String.valueOf(users.getUserid()));
			usersBean.setUsername(users.getUsername());
			if (users.getUserrank()==1) {
				usersBean.setUserrank("普通会员");
			} else if (users.getUserrank()==2) {
				usersBean.setUserrank("大众会员");
			}else if (users.getUserrank()==3) {
				usersBean.setUserrank("黄金会员");
			}else if (users.getUserrank()==4) {
				usersBean.setUserrank("白金会员");
			}else if (users.getUserrank()==5) {
				usersBean.setUserrank("砖石会员");
			}
			
			list2.add(usersBean);
		}
	}
	/**
	 * 会员详情：
	 * @param users
	 * @return
	 */
	public UsersBean findOneusers(Users users) {
		List<Users> list = dao.selectOneUser(users);
		List<UsersBean> list2=new LinkedList<>();
		status(list, list2);
		return list2.get(0);
	}
	/**
	 * 修改的方法：
	 * @param users
	 * @return
	 */
	public int updateUser(Users users) {
		if(users.getRankpoints()<=500) {
			users.setUserrank(1);
		}else if(users.getRankpoints()>500&&users.getRankpoints()<=1000) {
			users.setUserrank(2);
		}else if(users.getRankpoints()>1000&&users.getRankpoints()<=1500) {
			users.setUserrank(3);
		}else if(users.getRankpoints()>1500&&users.getRankpoints()<=2000) {
			users.setUserrank(4);
		}else if(users.getRankpoints()>2000&&users.getRankpoints()<=3000) {
			users.setUserrank(5);
		}
		return dao.updateuser(users);
	}
	/**
	 * 日期转换：
	 * @param addtime
	 * @return
	 */
	private String mydate(int addtime) {
		Calendar cal=Calendar.getInstance();
		long millions=new Long(addtime).longValue()*1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");//HH-mm-ss
		return df.format(cal.getTime());
	}
	/**
	 * 通过名字查找是否存在：
	 * @param users
	 * @return
	 */
	public Users findoneName(Users users) {
		return  dao.selectName(users).get(0);
	}
	/**
	 * 新增会员完善信息：
	 * @param users
	 * @return
	 */
	public int addUsers(Users users) {
		users.setPaypoints(0);
		users.setRankpoints(0);
		Calendar cl=Calendar.getInstance();
		int regtime=cl.get(Calendar.ALL_STYLES);
		users.setUserid(regtime);
		System.out.println(regtime);
		users.setRegtime(regtime);
		return dao.addUsers(users);
	}
	/**
	 * 会员排行信息：
	 * @return
	 */
	public PageBean<UsersBean> findAllUsers() {
		Map<Integer, List<Users>> selectBooks = dao.selectUsers();
		List<UsersBean> list2=new LinkedList<>();
		status(selectBooks.get(0), list2);
		PageBean<UsersBean> bean=new PageBean<UsersBean>();
		bean.setBeanList(list2);
		return bean;
	}
	/**
	 * 删除会员：
	 * @param userid
	 * @return
	 */
	public int deleteorders(int userid) {
		// TODO Auto-generated method stub
		return dao.deleteorders(userid);
	}
	
	

}
